package cc.spider.web.config;

import com.alibaba.fastjson.JSON;

import java.io.Serializable;
import java.util.HashMap;

public class Config implements Serializable {
    public transient static HashMap<String, String> SPIDER_TASKS_STRING = new HashMap<>();
    public transient static HashMap<String, Task> SPIDER_TASKS = new HashMap<>();
    private HashMap<String, Task> tasks = new HashMap<>();
    private String mainTask;

    public HashMap<String, Task> getTasks() {
        return tasks;
    }

    public void setTasks(HashMap<String, Task> tasks) {
        this.tasks = tasks;
        for (String key : tasks.keySet()) {
            SPIDER_TASKS_STRING.put(key, JSON.toJSONString(tasks.get(key)));
        }
        SPIDER_TASKS = this.tasks;
    }

    public String getMainTask() {
        return mainTask;
    }

    public void setMainTask(String mainTask) {
        this.mainTask = mainTask;
    }

    /**
     * 根据taskName创建一个task类，其属性配置与配置文件里的一样。
     * @param taskName
     * @return
     */
    public static Task newTask(String taskName) {
        Task task = JSON.parseObject(SPIDER_TASKS_STRING.get(taskName), Task.class);
        task.setTaskName(taskName);
        return task;
    }
}
